package com.brzjomo.embraceofthevoid.blocks.entity;

import com.brzjomo.embraceofthevoid.EmbraceOfTheVoid;
import com.brzjomo.embraceofthevoid.init.ModBlockEntityTypes;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.passive.VillagerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.World;

import java.util.regex.Pattern;

public class VillagerAltarEntity extends BlockEntity {

    public VillagerAltarEntity(BlockPos pos, BlockState state) {
        super(ModBlockEntityTypes.VILLAGER_ALTAR_ENTITY, pos, state);
    }

    public static void tick(World world, BlockPos pos, BlockState state, VillagerAltarEntity entity) {
        boolean shouldSpawn = world.getBlockState(pos.down()).getBlock() == Blocks.EMERALD_BLOCK;
        if (shouldSpawn && (world.getTime() % 60 == 0) && (Random.create().nextBetween(0, 9) < 2)) {
            VillagerEntity newSpawnVillager = new VillagerEntity(EntityType.VILLAGER, world);
            double XOffset = Random.create().nextBetween(0, 1);
            double ZOffset = Random.create().nextBetween(0, 1);
            if (XOffset == 0 && ZOffset == 0) {
                if (Random.create().nextBetween(0, 1) <= 0) {
                    XOffset = 1;
                } else {
                    ZOffset = 1;
                }
            }
            if (Random.create().nextBetween(0, 1) <= 0) {
                XOffset *= -1;
            }
            if (Random.create().nextBetween(0, 1) <= 0) {
                ZOffset *= -1;
            }
            XOffset += 0.5D;
            ZOffset += 0.5D;
            newSpawnVillager.setPos(pos.getX() + XOffset, pos.getY() + 0.5D, pos.getZ() + ZOffset);
            world.spawnEntity(newSpawnVillager);

            EmbraceOfTheVoid.LOGGER.info("EOTV: Spawned new villager at " + getWorldInfo(world.getDimensionKey().toString())
                    + "(" + logPosition(BlockPos.ofFloored(newSpawnVillager.getPos())) + ")");
        }
    }

    public static String getWorldID(String worldRegistryKey) {
        Pattern rule = Pattern.compile(":");
        String[] id = rule.split(worldRegistryKey);
        return id[id.length - 1].substring(0, id[id.length - 1].length() - 1);
    }

    public static String getWorldNameSpace(String worldRegistryKey) {
        Pattern rule = Pattern.compile(":");
        String[] id = rule.split(worldRegistryKey);

        Pattern rule2 = Pattern.compile(" ");
        id = rule2.split(id[id.length - 2]);
        return id[id.length - 1];
    }

    public static String getWorldInfo(String worldRegistryKey) {
        String id =  getWorldID(worldRegistryKey);
        String nameSpace = getWorldNameSpace(worldRegistryKey);
        return "[" + nameSpace + ":" + id + "]";
    }

    public static String logPosition(BlockPos pos) {
        return "X: " + pos.getX() + " , Y: " + pos.getY() + " , Z: " + pos.getZ();
    }

    protected Text getContainerName() {
        return Text.translatable("container." + EmbraceOfTheVoid.MOD_ID + ".villager_alter");
    }

    protected ScreenHandler createScreenHandler(int syncId, PlayerInventory playerInventory) {
//        return new FurnaceScreenHandler(syncId, playerInventory, this, this.propertyDelegate);
        return null;
    }

    @Override
    protected void writeNbt(NbtCompound nbt) {
        super.writeNbt(nbt);
    }

    @Override
    public void readNbt(NbtCompound nbt) {
        super.readNbt(nbt);
    }
}
